#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
  char name[20];
  int level;
   
  struct node *sibling;
  struct node *child;
};

struct node* initialise(char[],struct node*);
void insert(struct node *,char[],char[],int);
void delete(char[]);
void sinsert(struct node *,struct node *);
void display1(struct node *);
int main(){
  struct node *root;
  root=NULL;
  root=initialise("home",root);
  int level=0;
  char path[100],fname[30],result[100];  
  do{
    printf("Enter the child name along with path: or null to stop: ");
    scanf("%s",path);
    int i,j=0;
    char name1[20],pre[20];
    strcpy(name1," ");
    strcpy(pre," "); 
    //printf("len=%d",strlen(path));
    if((strcmp(path,"null"))!=0){
      for(i=0;i<=strlen(path);i++){
        if(path[i]=='/'){
            if(level==0){
              //root=initialise("home",root);
		name1[j]='\0';
              strcpy(pre,name1);
		strcpy(name1," ");
              level++;
	      j=0;
            } //end if
            else if(level>0){
		printf("before insert\n");
		printf("\npredecessor : %s",pre);
		printf("\nto be inserted %s",name1);
		printf("\n level to be inserted in : %d",level);
              insert(root,pre,name1,level);
		printf("after insert\n");
              strcpy(pre," ");
              strcpy(pre,name1);
              level++;
	      j=0; 
              strcpy(name1," ");          
            }//end else-if*/
            
        }
        else{
            name1[j]=path[i];
	    printf("\nextract : %c",name1[j]);
            j++;
        }

      }
       printf("\nend of if");
    }
   if(strcmp(path,"null")!=0) {
    printf("\n start of insert");
    insert(root,pre,name1,level);
    printf("after insert\n");
    printf("\npredecessor : %s",pre);
    printf("\nto be inserted %s",name1);
    printf("\n level to be inserted in : %d",level);    
    
    level=0;
  }
else 
return 0;
}while((strcmp(path,"null"))!=0); 
 //display function
   struct node *sib=root->child;
   struct node *sibc=sib->child;
  while(sib!=NULL)
  {
   printf("%s-->",sib->name);
   if(sibc!=NULL)
      printf("%s-->",sibc->name);
  while(sibc->child!=NULL) {
      sibc=sibc->child;
      printf("%s-->",sibc->name);
   } 
  sib=sib->sibling;
  if(sib!=NULL)
     sibc=sib->child;
}
   return 0;
 }
struct node* initialise(char n[10], struct node *root){
   printf("\nInitialised.");
   root=(struct node *)malloc(sizeof(struct node)); 
   strcpy(root->name,n);
   printf("%s\n",(root)->name);
   (root)->level=0;
   printf("%d\n",(root)->level);
   //(root)->fd=0;
   //printf("%d\n",(root)->fd);
   (root)->sibling=NULL;
   printf("%p\n",(root)->sibling);
   (root)->child=NULL;
   printf("%p\n",(root)->child);
   return root;
}

void insert(struct node *root,char pre[20],char name[20],int level){
  printf("\nI am in insert.");
   struct node *temp,*leaf;
   leaf=(struct node *)malloc(sizeof(struct node));
   temp=(struct node *)malloc(sizeof(struct node));      
   temp=root;
   
   
   strcpy(leaf->name,name);
   leaf->level=level;
   leaf->child=NULL;
   leaf->sibling=NULL;
   int i=0;
 //parent node
   while(/*(temp->child)!=NULL && */i<(level-1)){ 
     
     //if(i<level)
     temp=temp->child;
     i++;
     
   }

//sibling iteration  
   while((strcmp(temp->name,pre))!=0){
     temp=temp->sibling; 
   }

   if(temp->child==NULL)
    temp->child=leaf;    
   else{
   temp=temp->child;
	printf("\nshud print a : %s",temp->name);
   sinsert(temp,leaf);
   }
}
void sinsert(struct node *start,struct node *fresh){
   printf("I am in sinsert.");   
   struct node *temp;
  
   //temp=(struct node *)malloc(sizeof(struct node));
   
   temp=start;
   while(((temp->sibling)!=NULL) /*&& (strcmp(temp->name,fresh->name))!=0)*/){

     temp=temp->sibling;
   }
   if((strcmp(temp->name,fresh->name))!=0)
     temp->sibling=fresh;
}
      

     
/* void display1(struct node *root)
{
struct node *t=root;
while(t!=NULL)
{
	
	printf("%s",t->name);
        t=t->sibling;
 }
} */
